package com.zhuozhou.jdyc.main.settlement.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.bean.PopupWindowBean;
import com.zhuozhou.jdyc.databinding.SettlementShopItemBinding;
import com.zhuozhou.jdyc.main.goods.bean.BasicsCommodityBase;
import com.zhuozhou.jdyc.main.main.bean.BasicsShoppingBean;
import com.zhuozhou.jdyc.util.apiUtil.NumberUtils;
import com.zhuozhou.jdyc.wight.dialog.PickStringDialog;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by CherishTang on 2020/3/27 0027
 * describe:首页菜单adapter
 */
public class SettlementShopAdapter extends BaseRecyclerViewAdapter<BasicsShoppingBean.ObjBeanX.ObjBean, SettlementShopItemBinding> {

    public SettlementShopAdapter(Context context) {
        super(context);
    }

    public OnItemSetMoneyListener mOnItemSetMoneyListener;
    public PickStringDialog pickStringDialog;
    private List<PopupWindowBean> characterList;

    @Override
    protected int getLayoutId() {
        return R.layout.settlement_shop_item;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder baseViewHolder, int pos) {
        super.onBindViewHolder(baseViewHolder, pos);

    }

    @Override
    public void onBindHolder(BaseViewHolder<SettlementShopItemBinding> holder, int pos) {
        if (Integer.parseInt(mList.get(pos).getType()) == 1) {
            holder.getBinding().llCoupon.setVisibility(View.VISIBLE);
            holder.getBinding().llMoneyOld.setVisibility(View.VISIBLE);
        } else if (Integer.parseInt(mList.get(pos).getType()) == 2) {
            holder.getBinding().llCoupon.setVisibility(View.GONE);
            holder.getBinding().llMoneyOld.setVisibility(View.GONE);
        } else {
            holder.getBinding().llCoupon.setVisibility(View.GONE);
            holder.getBinding().llMoneyOld.setVisibility(View.GONE);
        }
        characterList = new ArrayList<>();
        for (BasicsCommodityBase.DataBean.DistributionBean item : mList.get(pos).getDistributionList()) {
            characterList.add(new PopupWindowBean(item.getId(), item.getName()));
        }

        SettlementShopItemAdapter settlementShopItemAdapter = new SettlementShopItemAdapter(mContext);
        settlementShopItemAdapter.setType(Integer.parseInt(mList.get(pos).getType()));
        settlementShopItemAdapter.setList(mList.get(pos).getShangpin());
        holder.getBinding().mRecyclerViewShop.setAdapter(settlementShopItemAdapter);
        holder.getBinding().mRecyclerViewShop.setLayoutManager(new MyLayoutManager(mContext));

        holder.getBinding().setBean(mList.get(pos));
        holder.getBinding().tvMoneyOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中间
        String[] strings = getAllMoney(mList.get(pos), 0);
        holder.getBinding().setPrice2((Integer.parseInt(mList.get(pos).getType()) == 1 ? "" : Integer.parseInt(mList.get(pos).getType()) == 2 ? (strings[1] + "积分+") :
                (strings[1] + "易货积分+")) + "￥" + strings[0]);
        holder.getBinding().tvDistribution.setOnClickListener(v -> {
            if (characterList != null) {
                pickStringDialog = new PickStringDialog(mContext, 1, characterList, (mCurrentPick, position, showText) -> {
                    holder.getBinding().tvDistribution.setText(showText);
                    holder.getBinding().tvDistribution.setTag(position);
                    settlementShopItemAdapter.setDistribution(position);
                    settlementShopItemAdapter.notifyDataSetChanged();
                    String[] string = getAllMoney(mList.get(pos), position);
                    holder.getBinding().setPrice2((Integer.parseInt(mList.get(pos).getType()) == 1 ? "" : Integer.parseInt(mList.get(pos).getType()) == 2 ? (string[1] + "积分+") :
                            (string[1] + "易货积分+")) + "￥" + string[0]);
                });
                pickStringDialog.create(null).show();
            }
        });
    }

    public void setOnItemSetMoneyListener(OnItemSetMoneyListener l) {
        mOnItemSetMoneyListener = l;
    }

    public interface OnItemSetMoneyListener {
        void onItemSetMoney(String[] str);
    }

    private String[] getAllMoney(BasicsShoppingBean.ObjBeanX.ObjBean obj, int DistributionId) {
        Double allMoney = 0.00;
        Double allIn = 0.00;
        String isAll = "true";
        int cont = 0;
        for (BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean shop : obj.getShangpin()) {
            if (Integer.parseInt(obj.getType()) == 1 || Integer.parseInt(obj.getType()) == 2) {
                allMoney = (allMoney + (shop.getNumber() * Double.parseDouble(shop.getGoods_price())));
                if (cont == 0 && obj.getDiscountSelect() != null && obj.getDiscountSelect().getPrice2() != null && allMoney >= Double.parseDouble(obj.getDiscountSelect().getPrice2())) {
                    allMoney = allMoney - Double.parseDouble(obj.getDiscountSelect().getPrice());
                    cont = 1;
                }
            }
            if (Integer.parseInt(obj.getType()) == 2)
                allIn = (allIn + (shop.getNumber() * Double.parseDouble(shop.getIntegral())));
            if (Integer.parseInt(obj.getType()) == 3)
                allIn = (allIn + (shop.getNumber() * Double.parseDouble(shop.getYibi())));
            if (DistributionId == 1) {
                allMoney = allMoney + Double.parseDouble(shop.getWuliu());
            }
            if (DistributionId == 4) {
                allMoney = allMoney + Double.parseDouble(shop.getHuoyun());
            }

        }
        if (mOnItemSetMoneyListener != null)
            mOnItemSetMoneyListener.onItemSetMoney(new String[]{NumberUtils.decimalFormat(String.valueOf(allMoney)), allIn == 0 ? "0" : allIn + "", isAll});
        return new String[]{NumberUtils.decimalFormat(String.valueOf(allMoney)), allIn == 0 ? "0" : allIn + "", isAll};
    }
}
